<template>
  <div>
    <el-form :model="form" ref="form" label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="角色名称:">
            <el-input
              v-model="form.roleName"
              placeholder="请输入角色名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="角色状态:">
            <el-select v-model="form.status" placeholder="请选择角色状态">
              <el-option
                :label="item.label"
                :value="item.value"
                v-for="(item, index) in statusList"
                :key="index"
              ></el-option> </el-select></el-form-item
        ></el-col>

        <el-col :span="6" style="margin: 4px 0 0 0px; padding-left: 28px">
          <span class="search"
            ><el-button
              size="medium"
              type="primary"
              @click="query"
              :loading="loading"
              >查 询</el-button
            ></span
          >
          <span
            ><el-button size="medium" plain @click="reset"
              >重 置</el-button
            ></span
          >
        </el-col>
      </el-row>
    </el-form>
    <div style="margin-bottom: 12px">
      <el-button size="medium" type="primary" @click="addData">新 增</el-button>
      <el-button size="medium" type="danger" @click="bathDelete"
        >删 除</el-button
      >
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        border
        row-key="Id"
        @selection-change="handleSelectionChange"
        :header-cell-style="{
          background: '#F5F7FA',
          color: '#303133',
        }"
      >
        <el-table-column type="selection" width="55"   :reserve-selection="true"> </el-table-column>
        <el-table-column label="序号" type="index" width="55">
        </el-table-column>
        <el-table-column prop="RoleName" label="角色名称" width="180">
        </el-table-column>

        <el-table-column label="角色状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.Status == 1">启用</el-tag>
            <el-tag type="danger" v-if="scope.row.Status == 0">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="Description" label="备注"> </el-table-column>
        <el-table-column prop="CreateByName" label="创建人" width="auto">
        </el-table-column>
        <el-table-column prop="CreateTime" label="创建时间" width="auto">
        </el-table-column>
        <el-table-column prop="UpdateByName" label="更新人" width="auto">
        </el-table-column>
        <el-table-column prop="UpdateTime" label="更新时间" width="auto">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="280px">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="details(scope.row)">
              <span style="color: #4965d2">详情</span></el-button
            >

            <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row)"
              style="padding-left: 12px"
            >
              <el-button type="text" size="small" slot="reference">
                <span style="color: #e0433f">删除</span></el-button
              >
            </el-popconfirm>
            <el-button type="text" size="small" @click="setData(scope.row)">
              <span style="color: #4965d2">设置权限</span></el-button
            >
            <el-button type="text" size="small" @click="association(scope.row)">
              <span style="color: #4965d2">关联用户</span></el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :total="pagerList.total"
          :page-size="pagerList.intPageSize"
          :current-page="pagerList.page"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </div>
    <addRole ref="addRole" @getData="getData" />
    <detailsRole ref="detailsRole" @getData="getData" />
  </div>
</template>

<script>
import {
  AccessRole_get,
  AccessRole_delete,
  AccessRole_BathDelete,
} from "@/api/business";
import addRole from "./addRole";
import detailsRole from "./detailsRole";
export default {
  name: "VueXuAdminMenu",
  components: {
    addRole,
    detailsRole,
  },
  data() {
    return {
      loading: false,
      form: {
        roleName: "",
        status: "",
      },
      pagerList: {
        intPageSize: 10,
        page: 1,
        total: 0,
      },
      statusList: [
        {
          value: 0,
          label: "禁用",
        },
        {
          value: 1,
          label: "启用",
        },
      ],
      tableData: [],
      ids: [],
    };
  },

  mounted() {
    this.getTableData();
  },

  methods: {
    setData(data) {
      this.$router.push({
        path: "/setPermissions",
      });
      localStorage.setItem("xmGlSzQx", JSON.stringify(data));
    },
    association(data){
      this.$router.push({
        path: "/association",
      });
      localStorage.setItem("xmGlSzQx", JSON.stringify(data));
    },
    getData() {
      this.getTableData();
    },
    addData() {
      this.$refs.addRole.show();
    },
    details(data) {
      this.$refs.detailsRole.show(data);
    },
    async del(data) {
      let res = await AccessRole_delete(data.Id);
      if (res.data.status == 200) {
        this.getTableData();
        this.$message({
          type: "success",
          message: "删除成功",
        });
      }
    },
    bathDelete() {
      if (this.ids.length == 0) {
        this.$message({
          type: "error",
          message: "至少选择一条数据!",
        });
        return;
      }
      this.$confirm("确定要批量删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await AccessRole_BathDelete(this.ids);
          if (res.data.status == 200) {
            this.getTableData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(data) {
      console.log(data);
      data.forEach((element) => {
        this.ids.push(element.Id);
      });
    },
    query() {
      this.loading = true;
      this.getTableData();
    },
    reset() {
      this.form = {
        roleName: "",
        status: "",
      };
      this.pagerList = {
        intPageSize: 10,
        page: 1,
        total: 0,
      };
      this.getTableData();
    },
    async getTableData() {
      this.form.managementId = JSON.parse(
        localStorage.getItem("xmGlDetails")
      ).Id;
      let value = Object.assign(this.form, this.pagerList);
      let res = await AccessRole_get(value);
      console.log(res, "123456");
      if (res.data.status == 200) {
        this.loading = false;
        this.tableData = res.data.response.data;
        this.pagerList.total = res.data.response.dataCount;
      } else {
        this.loading = false;
        this.$message({
          type: "error",
          message: res.data.msg,
        });
      }
    },
    currentChange(page) {
      this.pagerList.page = page;
      // this.getTableData();
    },
  },
};
</script>

<style scoped>
.pagination {
  float: right;
  margin: 24px 0px;
}
/deep/ .el-select {
  width: 100%;
}
</style>
